﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Data.SqlClient;

namespace Scheduler.Data_Access_Layer
{
    /**Class: ScheduleDisplayer
    * @author Daniel Chopson
    * @version 1.0
    * Course : ITEC 4860 Spring 2012
    * Written: March 22, 2012
    *
    * This class connects to the ScheduleDay and Shift tables in the database via TableAdapters.
    * Getters and setters are provided for shifts.
    *
    * Purpose: To provide the data access neccessary for displaying a schedule.
    */

    class ScheduleDisplayer
    {
        //The scheduled date

        private DateTime schedDate;

        //The company's ID

        private string companyID;

        //The employee's ID

        private string empUserID;

        //The adapter for ScheduleDay table connectivity

        private SchedulerTableAdapters.ScheduleDayTableAdapter schedDayAdapter;

        //The adapter for Shift table connectivity

        private SchedulerTableAdapters.ShiftTableAdapter shiftAdapter;

        //The data table for temporarily holding shift data

        private Scheduler.ShiftDataTable shiftData;

        //A single row from the Shift table

        private Scheduler.ShiftRow shiftRow;

        /** Method: ScheduleDisplayer
          * Constructor for this class. Instantiates all class variables
          * @param schedDate the scheduled date
          * @param companyID the company's ID
          * @param empUserID the employee's ID
          */

        public ScheduleDisplayer(DateTime schedDate, string companyID, string empUserID)
        {
            this.schedDate = schedDate;
            this.companyID = companyID;
            this.empUserID = empUserID;
            schedDayAdapter = new SchedulerTableAdapters.ScheduleDayTableAdapter();
            shiftAdapter = new SchedulerTableAdapters.ShiftTableAdapter();
            shiftData = shiftAdapter.GetData();
        }

        /** Method: createSchedDay
          * Creates a record in the ScheduleDay table
          * @return an ArrayList of SQL error messages for error handling in the Presentation_Layer
          */

        public ArrayList createSchedDay()
        {
            ArrayList errorMessages = new ArrayList();
            try
            {
                schedDayAdapter.Insert(schedDate, companyID);
            }
            catch (SqlException exception)
            {
                for (int i = 0; i < exception.Errors.Count; i++)
                {
                    ErrorLookup errorLookup = new ErrorLookup(exception.Errors[i].Number);
                    errorMessages.Add(errorLookup.returnSimpleError());
                }
            }
            return errorMessages;
        }

        /** Method: createShift
          * Creates a record in the Shift table
          * @param startTime the shift starting time
          * @param endTime the shift ending time
          * @param isReleased was the shift released?
          * @return an ArrayList of SQL error messages for error handling in the Presentation_Layer
          */

        public ArrayList createShift(TimeSpan startTime, TimeSpan endTime, bool isReleased)
        {
            ArrayList errorMessages = new ArrayList();
            try
            {
                shiftAdapter.Insert(schedDate, empUserID, startTime, endTime, isReleased);
            }
            catch (SqlException exception)
            {
                for (int i = 0; i < exception.Errors.Count; i++)
                {
                    ErrorLookup errorLookup = new ErrorLookup(exception.Errors[i].Number);
                    errorMessages.Add(errorLookup.returnSimpleError());
                }
            }
            return errorMessages;
        }

        /** Method: updateShift
          * Updates an existing record in the Shift table
          * @param origSchedDate the SchedDate key for the record to be changed
          * @param origEmpUserID the EmpUserID key for the record to be changed
          * @param schedDate the new SchedDate key for the record to be changed
          * @param empUserID the new EmpUserID key for the record to be changed
          * @param startTime the shift starting time
          * @param endTime the shift ending time
          * @param isReleased was the shift released?
          * @return an ArrayList of SQL error messages for error handling in the Presentation_Layer
          */

        public ArrayList updateShift(DateTime schedDate, string empUserID, TimeSpan startTime, TimeSpan endTime, bool isReleased)
        {
            ArrayList errorMessages = new ArrayList();
            try
            {
                shiftAdapter.UpdateShift(startTime.ToString(), endTime.ToString(), isReleased, schedDate.ToString(), empUserID);
            }
            catch (SqlException exception)
            {
                for (int i = 0; i < exception.Errors.Count; i++)
                {
                    ErrorLookup errorLookup = new ErrorLookup(exception.Errors[i].Number);
                    errorMessages.Add(errorLookup.returnSimpleError());
                }
            }
            return errorMessages;
        }

        /** Method: getAllShifts
          * Gets all of the fields for a given row
          * @return an ArrayList of database fields
          */

        public ArrayList getAllShifts()
        {
            ArrayList shiftArray = new ArrayList();
            foreach (Scheduler.ShiftRow shiftRow in shiftData)
            {
                ArrayList temp = new ArrayList();
                temp.Add(shiftRow.SchedDate);
                temp.Add(shiftRow.EmpUserID);
                temp.Add(shiftRow.StartTime);
                temp.Add(shiftRow.EndTime);
                temp.Add(shiftRow.IsReleased);
                shiftArray.Add(temp);
            }
            return shiftArray;
        }

        /** Method: locateShift
          * Locates the row in the Shift table with the specified SchedDate and EmpUserID
          * Note: Must be called before the methods following it can be used
          * @return a boolean to denote whether the shift was found
          */

        public bool locateShift()
        {
            shiftData = shiftAdapter.GetDataBySchedDateEmpID(schedDate.ToString(), empUserID);
            if (shiftData.Count == 0)
            {
                return false;
            }
            else
            {
                shiftRow = shiftData[0];
            }
            return true;
        }

        /** Method: getStartTime
          * Gets the shift start time
          * @return StartTime from the Shift table
          */

        public TimeSpan getStartTime()
        {
            return shiftRow.StartTime;
        }

        /** Method: setStartTime
          * Sets the shift start time
          * @param startTime the shift start time
          */

        public void setStartTime(TimeSpan startTime)
        {
            shiftRow.StartTime = startTime;
            shiftAdapter.Update(shiftData);
        }

        /** Method: getEndTime
          * Gets the shift end time
          * @return EndTime from the Shift table
          */

        public TimeSpan getEndTime()
        {
            return shiftRow.EndTime;
        }

        /** Method: setEndTime
          * Sets the shift end time
          * @param endTime the shift end time
          */

        public void setEndTime(TimeSpan endTime)
        {
            shiftRow.EndTime = endTime;
            shiftAdapter.Update(shiftData);
        }

        /** Method: getIsReleased
          * Gets the shift is released flag
          * @return IsReleased from the Shift table
          */

        public bool getIsReleased()
        {
            return shiftRow.IsReleased;
        }

        /** Method: setIsReleased
          * Sets the shift is released flag
          * @param isReleased the shift is released flag
          */

        public void setIsReleased(bool isReleased)
        {
            shiftRow.IsReleased = isReleased;
            shiftAdapter.Update(shiftData);
        }
    }
}
